"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 空字符串
Case Name   : postgresql.conf文件修改参数值
Create At   : 2023.09.12
Owner       : z@wan005
Description :
    1. postgresql.conf文件设置参数值
    2. 清理环境
Expect      :
    1. 成功
    2. 成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class AcceptEmptyStr(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')

    def test_null(self):
        text = '--step1:postgresql.conf文件设置参数值;expect:成功--'
        self.log.info(text)
        sed_cmd = f"sed -i '$a\\behavior_compat_options=accept_empty_str' " \
                  f"{macro.DB_INSTANCE_PATH}/postgresql.conf"
        self.log.info(sed_cmd)
        sql_res = self.pri_node.sh(sed_cmd).result()
        self.log.info(sql_res)
        msg = self.pri_sh.restart_db_cluster()
        self.log.info(msg)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)
        sql_cmd = "show behavior_compat_options"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual('accept_empty_str', sql_res.splitlines()[-2].strip(),
                         '查询失败')

    def tearDown(self):
        text = '----step2:清理环境;expect:成功----'
        self.log.info(text)
        sed_cmd = f"source {macro.DB_ENV_PATH};" \
                  f"sed -i '$d' {macro.DB_INSTANCE_PATH}/postgresql.conf"
        self.log.info(sed_cmd)
        sql_res = self.pri_node.sh(sed_cmd).result()
        self.log.info(sql_res)
        msg = self.pri_sh.restart_db_cluster()
        self.log.info(msg)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
